#include "haed.h"


//服务器

int main()
{

	struct sockaddr_in soc;
	int fd = -1;

	//创建套接字
	if(fd = socket(AF_INET, SOCK_DGRAM, 0) < 0);
	{
		perror("socket");
		exit(0);
	}	
	printf("fd = %d", fd);

	//将网络地址结构和套接字的文件描述符
	soc.sin_family = AF_INET;
	soc.sin_port = htons(SERV_PORT);
#if 0
	soc.sin_addr.s_addr = inet_addr(SERV_IP_ADDR);
#else
	if((inet_pton(AF_INET, SERV_IP_ADDR，(void* )&soc.sin_addr.s_addr)) != 1)
	{
		peeror("inet_pton");
		exit(0);
	}
#endif

	//绑定
	if( (bind(fd, (struct sockaddr* )&soc, sizeof(soc)) < 0))
	{
		perror("bin");
		exit(0);
	}

	//将主动套接字变成被动套接字
	if((listen(fd, BACKLOG)) < 0)
	{
		perror("listen");
		exit(0);
	}
	printf("server starting...ok !\n");


	int newfd = -1;
	//阻塞等待客户端的连接
	if(( newfd = accept(fd, NULL, NULL)) < 0)
	{
		perror("accept");
		exit(0);
	}


	/* 读写 */
	int ret = -1;
	char buf[BUFSI] = {0};
	read(newfd, buf, BUFSI-1);

	strcat(buf, "---xiaoyang");
	write(newfd, buf, BUFSI-1);


	sleep(10);

	close(newfd);
	close(fd);
	return 0;
}














